package cn.zhangtiancinb.simple_demo.http.controller;

import java.util.Date;
import java.util.UUID;
import java.nio.charset.StandardCharsets;

import javax.annotation.Resource;

import lombok.extern.slf4j.Slf4j;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.GetResponse;
import com.rabbitmq.client.ConnectionFactory;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.boot.autoconfigure.amqp.RabbitProperties;

/**
 * 消息队列
 *
 * @author 张天赐
 */
@Slf4j
@RestController
@RequestMapping("/mq")
public class MessageQueueController {

    @Resource
    RabbitProperties rabbitProperties;
    @Resource
    RabbitTemplate rabbitTemplate;

    @Value("${topic.auto_consume:AUTO_CONSUME_TOPIC}")
    String autoConsumeTopic;

    /**
     * 生产消息
     *
     * @return 生产结果
     */
    @RequestMapping("produce")
    public String producer(@RequestParam(required = false, defaultValue = "topic") String topic,
        @RequestParam(required = false, defaultValue = "content") String content) {
        Message message = new Message(content.getBytes(), new MessageProperties() {{
            setTimestamp(new Date());
            setMessageId(String.valueOf(UUID.randomUUID()));
        }});
        rabbitTemplate.convertAndSend(topic, message);
        return "OK";
    }

    /**
     * 生产消息 - 自动消费
     *
     * @return 生产结果
     */
    @RequestMapping("produce_auto_consume")
    public String producer(@RequestParam(required = false, defaultValue = "content") String content) {
        Message message = new Message(content.getBytes(), new MessageProperties() {{
            setTimestamp(new Date());
            setMessageId(String.valueOf(UUID.randomUUID()));
        }});
        rabbitTemplate.convertAndSend(autoConsumeTopic, message);
        return "OK";
    }

    /**
     * 生产消息
     *
     * @return 生产结果
     */
    @RequestMapping("consume")
    public String consume(@RequestParam(required = false, defaultValue = "topic") String topic) throws Exception {
        ConnectionFactory connectionFactory = new ConnectionFactory() {{
            setHost(rabbitProperties.getAddresses());
            setPort(rabbitProperties.getPort());
            setUsername(rabbitProperties.getUsername());
            setPassword(rabbitProperties.getPassword());
        }};
        Connection connection = connectionFactory.newConnection();
        Channel channel = connection.createChannel();
        GetResponse response = channel.basicGet(topic, true);
        if (response == null) {
            return "NOTHING";
        } else {
            String content = new String(response.getBody(), StandardCharsets.UTF_8);
            return "OK:" + content;
        }
    }
}
